<template>
  <div class="workEdit-box">
    <!-- 项目经历模块 -->
    <div class="workEdit-min-box">
      <!-- 项目经历：时间-项目名称 -->
      <div
        class="project-header"
        @mouseenter="onMouseEnter"
        @mouseleave="onMouseLeave"
      >
        <TitleD
          :attr1="project.time"
          :attr2="project.proName"
          @attrUpdate="onProjectUpdate"
        ></TitleD>
        <div class="tool-box" v-if="toolBoxVisiable">
          <el-button
            circle
            icon="el-icon-remove-outline"
            size="mini"
            class="tool-item"
            @click="btnClick('delete')"
            :disabled="delDisAbled"
          ></el-button>
          <el-button
            circle
            icon="el-icon-circle-plus-outline"
            size="mini"
            class="tool-item"
            @click="btnClick('add')"
            :disabled="addDisAbled"
          ></el-button>
        </div>
      </div>
      <div>
        <AutoTextArea
          v-for="(desc, index) in project.proDesc"
          :key="index"
          :data="desc"
          shape="point"
          @textChange="onDescChange(index,$event)"
        ></AutoTextArea>
      </div>
    </div>
  </div>
</template>

<script>
import AutoTextArea from '@/components/base/AutoTextArea.vue'
import TitleC from '@/components/base/TitleC.vue'
import TitleD from '@/components/base/TitleD.vue'
export default {
  name: 'Project',
  data() {
    return {
      // 内容是否可编辑
      toolBoxVisiable: false,
      project: {
        id: '',
        // 项目时间
        time: '',
        // 项目名称
        proName: '',
        // 工作描述
        proDesc: [],
      },
      addDisAbled: false,
      delDisAbled: false,
    }
  },
  props: {
    experience: {},
  },
  watch: {
    'project.proDesc': {
      deep: true,
      immediate: true,
      handler(newVal) {
        if (newVal.length == 1) this.delDisAbled = true
        else if (newVal.length >= 5) this.addDisAbled = true
        else {
          this.delDisAbled = false
          this.addDisAbled = false
        }
      },
    },
  },

  methods: {
    // 项目标题内容修改（项目时间、项目名称）
    onProjectUpdate({ index, newVal }) {
      switch (index) {
        case 1:
          this.project.time = newVal
          this.projectExperienceDataChange()
          break
        case 2:
          this.project.proName = newVal
          this.projectExperienceDataChange()
          break
      }
    },
    // 工作经历组件数据改变
    // 告知父组件，更新数据
    projectExperienceDataChange() {
      this.$emit('projectInfoUpdate', this.project)
    },
    // 描述内容修改
    onDescChange(jobDescIndex,newVal) {
      this.project.proDesc[jobDescIndex] = newVal
      this.projectExperienceDataChange()
    },
    // 鼠标移入显示工具栏
    onMouseEnter() {
      this.toolBoxVisiable = true
    },
    // 鼠标移出隐藏工具栏
    onMouseLeave() {
      this.toolBoxVisiable = false
    },
    btnClick(tag) {
      if (tag === 'add') {
        this.addProDesc()
      } else {
        this.delProDesc()
      }
    },
    // 添加一条项目描述
    addProDesc() {
      if (this.project.proDesc.length >= 5) {
        this.$message.warning('最多添加五条数据')
        return
      }
      const proDesc = '项目一级棒'
      this.project.proDesc.push(proDesc)
      this.projectExperienceDataChange()
    },
    // 删除一条项目描述
    delProDesc() {
      if (this.project.proDesc.length <= 1) {
        this.$message.warning('至少需要一条数据')
        return
      }
      this.project.proDesc.pop()
      this.projectExperienceDataChange()
    },
  },
  components: {
    AutoTextArea,
    TitleC,
    TitleD,
  },
  created() {
    this.project = this.experience
  },
}
</script>

<style lang="less" scoped>
.workEdit-box {
  width: 100%;
  padding: 0 15px;
}
.project-header {
  position: relative;
}
.icon-box {
  display: flex;
  align-items: center;
  border: 1px #eae4e4 solid;
  border-radius: 3px;
  cursor: pointer;
}
.tool-box {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 80px;
  height: 40px;
  // font-size: 30px;
  line-height: 40px;
  text-align: center;
}
.tool-item {
  border: none;
  padding: 0;
}
.el-button--mini {
  font-size: 20px;
}
</style>
